package com.ouc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ouc.common.Result;
import com.ouc.entity.*;
import com.ouc.service.CartItemService;
import com.ouc.service.FrameService;
import com.ouc.service.LineService;
import com.ouc.service.ShapeService;
import com.ouc.util.Encoding;
import com.ouc.vo.OutCartItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Name: CarController
 * @Description:
 * @Author: Mr.Tong
 */
@RestController
@RequestMapping("/car")
@CrossOrigin
@Api(tags = "购物车接口", description = "提供购物车的接口信息")
public class CartController {
    @Autowired
    private Encoding encoding;
    @Autowired
    private FrameService frameService;
    @Autowired
    private LineService lineService;
    @Autowired
    private ShapeService shapeService;
    @Autowired
    private CartItemService cartItemService;

    @ApiOperation("添加产品到购物车")
    @PostMapping("/insert")
    public Result insert(@RequestBody CartItem cartItem) {
        Frame frame = cartItem.getFrame();
        Shape shape = frame.getShape();
        String shapeCode = encoding.shapeEncoding(shape);
        shape.setShapeCode(shapeCode);
        String lineCode = encoding.lineEncoding(frame.getLine());
        String frameCode = shapeCode + lineCode;

        frame.setFrameCode(frameCode);
        frame.setLineCode(lineCode);
        frame.setShapeCode(shapeCode);
        Line line = lineService.getLineByLineCode(lineCode);
        frame.setLine(line); //设置为从数据库取到的Line

        cartItem.setFrameCode(frameCode);

        if (!frameService.frameExists(frameCode)) {
            if (!shapeService.shapeExists(shapeCode)) {
                shapeService.insertShape(shape);
            } else {
                //no op
            }
            frameService.insertFrame(frame);
        } else {
            //no op
        }
        if (insertCartItem(cartItem)) {
            return Result.ok().data("description", "添加到购物车成功");
        }
        return Result.error().data("description", "添加到购物车失败");
    }


    /**
     * @Description: 插入到cartitem表中的操作，购物车中如果有，那么就更新数据，如果没有的话就需要进行插入
     * @Author: Mr.Tong
     * modified by Xu.
     */
    private Boolean insertCartItem(CartItem cartItem) {
        //插入到购物车中，在插入之前先进行检查(在当前用户的购物车中进行检查，是否已经存在所选的Frame)
        CartItem itemInDb = cartItemService.getItem(cartItem.getFrameCode(), cartItem.getUserId());
        if (Objects.equals(itemInDb, null)) {//如果购物车中没有
            return cartItemService.insertItem(cartItem);
        } else {
            cartItem.addQuantity(itemInDb.getQuantity());
            return cartItemService.updateItem(cartItem);
        }
    }


    @ApiOperation("根据用户id获取当前用户所有的购物车内容，添加mybatis的分页插件")
    @GetMapping("/getallbypaging/{page}/{limit}")
    public Result getCartItemByUserIdAndPaging(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @RequestParam(name = "userId")
                    Integer userId,
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable(name = "page")
                    Integer page,
            @ApiParam(name = "limit", value = "每页显示的记录数", required = true)
            @PathVariable(name = "limit")
                    Integer limit
    ) {
        List<OutCartItem> allCartItemsByPaging = cartItemService.getAllCartItemsByPaging(userId, page, limit);
        return Result.ok()
                .data("curpageitems", allCartItemsByPaging)
                .data("allitems", cartItemService.getAllCartItems(userId).size());
    }


}
